package com.itcat.api;/*
 * @Author: gonhsl
 * @Date:2022-04-02-17:09
 * */

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itcat.api.mysql.SmsCouponApi;
import com.itcat.mapper.SmsCouponMapper;
import com.itcat.mapper.SmsCouponProductCategoryRelationMapper;
import com.itcat.mapper.SmsCouponProductRelationMapper;
import com.itcat.mysql.param.SmsCouponParam;
import com.itcat.mysql.pojo.SmsCoupon;
import com.itcat.mysql.pojo.SmsCouponProductCategoryRelation;
import com.itcat.mysql.pojo.SmsCouponProductRelation;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.stream.Collectors;

@DubboService
public class SmsCouponApiImpl implements SmsCouponApi {
    @Autowired
    private SmsCouponMapper smsCouponMapper;
    @Autowired
    private SmsCouponProductCategoryRelationMapper smsCouponProductCategoryRelationMapper;
    @Autowired
    private SmsCouponProductRelationMapper smsCouponProductRelationMapper;

    @Override
    public Page<SmsCoupon> findList(Integer pageNum, Integer pageSize, String name, Integer type) {
        Page<SmsCoupon> smsCouponPage = new Page<>(pageNum,pageSize);
        LambdaQueryWrapper<SmsCoupon> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(name!=null, SmsCoupon::getName,name)
                .eq(type!=null, SmsCoupon::getType,type);
        Page<SmsCoupon> page = smsCouponMapper.selectPage(smsCouponPage, wrapper);
        return page;

    }

    @Override
    public void create(SmsCouponParam couponParam) {
        SmsCoupon smsCoupon = new SmsCoupon();
        BeanUtils.copyProperties(couponParam, smsCoupon);
        smsCouponMapper.insert(smsCoupon);

        List<SmsCouponProductRelation> relationList = couponParam.getProductRelationList();
        relationList.stream().map((item) -> {
            smsCouponProductRelationMapper.insert(item);
            return item;
        }).collect(Collectors.toList());
        List<SmsCouponProductCategoryRelation> categoryRelationList = couponParam.getProductCategoryRelationList();
        categoryRelationList.stream().map((item) -> {
            smsCouponProductCategoryRelationMapper.insert(item);
            return item;
        }).collect(Collectors.toList());


    }

    @Override
    public void delete(Integer id) {
        smsCouponMapper.deleteById(id);
        LambdaQueryWrapper<SmsCouponProductRelation> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SmsCouponProductRelation::getCouponId,id);
        smsCouponProductRelationMapper.delete(wrapper);
        LambdaQueryWrapper<SmsCouponProductCategoryRelation> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SmsCouponProductCategoryRelation::getCouponId,id);
        smsCouponProductCategoryRelationMapper.delete(queryWrapper);
    }

    @Override
    public void update(SmsCouponParam couponParam, Integer id) {
        SmsCoupon smsCoupon = new SmsCoupon();
        BeanUtils.copyProperties(couponParam,smsCoupon);
        LambdaUpdateWrapper<SmsCoupon> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(SmsCoupon::getId,id).setEntity(couponParam);
        smsCouponMapper.update(smsCoupon,updateWrapper);

        List<SmsCouponProductRelation> relationList = couponParam.getProductRelationList();
        relationList.stream().map((item)->{
            LambdaUpdateWrapper<SmsCouponProductRelation> updateWrapper1 = new LambdaUpdateWrapper<>();
            updateWrapper1.eq(SmsCouponProductRelation::getCouponId,id).setEntity(item);
            smsCouponProductRelationMapper.update(item,updateWrapper1);

            return item;
        }).collect(Collectors.toList());
        List<SmsCouponProductCategoryRelation> categoryRelationList = couponParam.getProductCategoryRelationList();
        categoryRelationList.stream().map((item)->{
            LambdaUpdateWrapper<SmsCouponProductCategoryRelation> updateWrapper1 = new LambdaUpdateWrapper<>();
            updateWrapper1.eq(SmsCouponProductCategoryRelation::getCouponId,id).setEntity(item);
            smsCouponProductCategoryRelationMapper.update(item,updateWrapper1);
            return item;
        }).collect(Collectors.toList());
    }

    @Override
    public SmsCouponParam findOne(Integer id) {

        LambdaQueryWrapper<SmsCoupon> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SmsCoupon::getId,id);
        SmsCoupon smsCoupon = smsCouponMapper.selectOne(wrapper);

        LambdaQueryWrapper<SmsCouponProductRelation> wrapper1 = new LambdaQueryWrapper<>();
        wrapper1.eq(SmsCouponProductRelation::getCouponId,id);
        List<SmsCouponProductRelation> relationList = smsCouponProductRelationMapper.selectList(wrapper1);

        LambdaQueryWrapper<SmsCouponProductCategoryRelation> wrapper2 = new LambdaQueryWrapper<>();
        wrapper2.eq(SmsCouponProductCategoryRelation::getCouponId,id);
        List<SmsCouponProductCategoryRelation> categoryRelations = smsCouponProductCategoryRelationMapper.selectList(wrapper2);

        SmsCouponParam smsCouponParam = new SmsCouponParam();
        BeanUtils.copyProperties(smsCoupon,smsCouponParam);
        smsCouponParam.setProductRelationList(relationList);
        smsCouponParam.setProductCategoryRelationList(categoryRelations);
        return smsCouponParam;
    }
}

